%%%-----------------------------------------------------------------------------
%%% @doc
%%% apn_server  connect manager.
%%%
%%% @end
%%%-----------------------------------------------------------------------------
-module (apn_server_connect_manager).
-behaviour(gen_server).

-export([start_link/1, create_connect/2]).

-include("apns.hrl").

%% gen_server Function Exports
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-record(state, {}).

create_connect(AppKey, Type) ->
    gen_server:call(?MODULE, {create_connect, AppKey, Type}, 30000).

start_link(CertFileOpts) ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [CertFileOpts], []).


%%%=============================================================================
%%% gen_server callbacks
%%%=============================================================================

init([{DevCertFileOpts, ProCertFileOpts}]) ->
    {ok, DevCertFileList} = file:list_dir(DevCertFileOpts),
    {ok, ProCertFileList} = file:list_dir(ProCertFileOpts),
    lists:foreach(
        fun(CertFile) -> 
            case string:tokens(CertFile, ".") of
                [File, "pem"] ->
                    Connection = apn_server_core:dev_default_connection(),
                    Connection2 = Connection#apns_connection{cert_file = Connection#apns_connection.cert_file ++ CertFile},
                    apn_server_connection:start_link(list_to_atom(File), Connection2);
                _ ->
                    ok
            end
        end, DevCertFileList),
    
    lists:foreach(
        fun(CertFile) -> 
            case string:tokens(CertFile, ".") of
                [File, "pem"] ->
                    Connection = apn_server_core:pro_default_connection(),
                    Connection2 = Connection#apns_connection{cert_file = Connection#apns_connection.cert_file ++ CertFile},
                    apn_server_connection:start_link(list_to_atom(File), Connection2);
                _ ->
                    ok
            end
        end, ProCertFileList),
    {ok, #state{}}.

handle_call({create_connect, AppKey, Type}, _From, State) ->
    Connection = case Type of
        "dev" ->
            apn_server_core:dev_default_connection();
        "pro" ->
            apn_server_core:pro_default_connection();
        _ ->
            apn_server_core:dev_default_connection()
    end,
    Connection2 = Connection#apns_connection{cert_file = Connection#apns_connection.cert_file ++ AppKey ++ ".pem"},
    Reply = apn_server_connection:start_link(list_to_atom(AppKey), Connection2),
    {reply, Reply, State};

handle_call(Req, _From, State) ->
    lager:error("unexpected request: ~p", [Req]),
    {reply, {error, badreq}, State}.

handle_cast(_Msg, State) ->
    {noreply, State}.

handle_info(_Info, State) ->
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.


